package com.stxx.stiot2.service.tran;

import com.stxx.framwork.tran.TranBean;
import com.stxx.framwork.tran.TranService;
import com.stxx.framwork.tran.TranUtils;
import com.stxx.framwork.utils.StoreUtils;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.sql.SqlBuilder;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TerminalTranService implements TranService {
    @Inject
    StoreUtils storeUtils;
    @Override
    public Map<String, Map<String, Object>> tran(String fieldName,Collection<String> ids) {
            SqlBuilder sqlBuilder = new SqlBuilder();
            sqlBuilder.append("select t.code as id,t.name as terminal_name,p.code as profile_code,p.name as profile_name from ")
                    .append(StoreUtils.getTableName("terminal")).append(" t")
                    .append(" left join ").append(StoreUtils.getTableName("profile")).append(" p")
                    .append(" on p.code = t.profile_code")
                    .append(" where t.code in(?...)",ids);
            Map<String, Map<String,Object>> map = new HashMap<>();
            try {
                List<Map> list1 = storeUtils.sql(sqlBuilder).queryRowList(Map.class);
                if(list1!=null){
                    list1.forEach(item->{
                        Map<String,Object> tranMap = new HashMap<>();
                        tranMap.put(fieldName,item.get("terminalName"));
                        tranMap.put("terminalName",item.get("terminalName"));
                        tranMap.put("profileCode",item.get("profileCode"));
                        tranMap.put("profileName",item.get("profileName"));
                        map.put(item.get("id").toString(),tranMap);
                    });
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return map;
    }

}
